<script lang="ts" setup>
import { ref } from 'vue';
import { onShow } from '@dcloudio/uni-app'
import { trainingScanCodeSignin, trainingGetMyCourseInfo } from '/api/provide';
import { userInfo } from "/stores/user";

const data = ref({})
onShow(async () => {
	uni.showLoading()
	const res = await trainingGetMyCourseInfo()
	uni.hideLoading();
	data.value = res.data || {};
})

async function scanCode() {
	const { result } = await uni.scanCode({
		onlyFromCamera: true,
		scanType: ['qrCode'],
	})
	uni.showLoading();
	await trainingScanCodeSignin({ trainingCourseId: result });
	uni.showToast({
		title: '签到成功',
	});
}
</script>

<template>
	<view class="T_page">
		<view class="face" :style="`padding-top:${uni.$navBarHeight}px`">
			<view class="user" @click="uni.navigateTo({ url: '/pages/baseInfo/index' })">
				<image src="/static/svg/avatar-default.svg" />
				<text>{{ userInfo.teacherName }}</text>
			</view>
			<view class="right">
				<uni-icons type="scan" :size="uni.$rpxToPx(40)" color="#9CA3AF" @click="scanCode" />
				<view @click="uni.navigateTo({ url: '/pages/notice/index' })">
					<uni-badge :text="data.msgNumber" type="error" absolute="rightTop">
						<uni-icons type="notification" :size="uni.$rpxToPx(40)" color="#9CA3AF" />
					</uni-badge>
				</view>
			</view>
		</view>
		<view class="card">
			<view>
				<view>{{ data.socre || 0 }}</view>
				<text>获得学分</text>
			</view>
			<view>
				<view>{{ data.noWorknumber || 0 }}</view>
				<text>待提交作业</text>
			</view>
		</view>
		<view class="list">
			<view @click="uni.navigateTo({ url: '/pages/editInfo/index' })">
				<image class="icon" src="/static/svg/info.svg" />
				<text>信息编辑</text>
				<uni-icons type="right" :size="uni.$rpxToPx(30)" color="#D1D5DB" />
			</view>
			<view @click="uni.navigateTo({ url: '/pages/homework/index' })">
				<image class="icon" src="/static/svg/homework.svg" />
				<text>培训作业</text>
				<uni-icons type="right" :size="uni.$rpxToPx(30)" color="#D1D5DB" />
			</view>
			<view @click="uni.navigateTo({ url: '/pages/myCredit/index' })">
				<image class="icon" src="/static/svg/credit.svg" />
				<text>我的学分</text>
				<uni-icons type="right" :size="uni.$rpxToPx(30)" color="#D1D5DB" />
			</view>
			<view @click="uni.navigateTo({ url: '/pages/appSetting/index' })">
				<image class="icon" src="/static/svg/tip.svg" />
				<text>应用设置</text>
				<uni-icons type="right" :size="uni.$rpxToPx(30)" color="#D1D5DB" />
			</view>
		</view>
	</view>
</template>

<style lang="scss" scoped>
.T_page {
	>.face {
		padding: 0 20rpx 50rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		gap: 30rpx;

		>.user {
			display: flex;
			align-items: center;
			gap: 30rpx;

			>image {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
				background-color: #fff;
				flex-shrink: 0;
				padding: 10rpx;
				box-sizing: border-box;
			}

			>text {
				flex-grow: 1;
				font-weight: bold;
				font-size: 30rpx;
			}
		}

		>.right {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			gap: 40rpx;
		}
	}

	>.card {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 30rpx;
		height: 150rpx;

		>view {
			background-color: #fff;
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			gap: 10rpx;

			&:nth-child(1)>view {
				color: #3B82F6;
			}

			&:nth-child(2)>view {
				color: #22C55E;
			}

			>view {
				font-size: 40rpx;
				font-weight: bold;
			}

			>text {
				font-size: 24rpx;
				color: #6B7280;
			}
		}
	}

	>.list {
		background-color: #fff;
		border-radius: 20rpx;
		margin-top: 30rpx;
		padding: 0 20rpx;

		>view {
			height: 110rpx;
			display: flex;
			align-items: center;
			gap: 20rpx;

			&+view {
				border-top: 1rpx solid #f8f8f8;
			}

			>.icon {
				width: 30rpx;
				height: 30rpx;
				margin-left: 10rpx;
			}

			>text {
				flex-grow: 1;
				font-size: 28rpx;
				color: #374151;
			}
		}
	}
}
</style>
